//
//  SQLiteFsRepositoryTests.cpp
//  FuseSQLite
//
//  Created by Admin on 7/20/14.
//  Copyright (c) 2014 Admin. All rights reserved.
//

#include "SQLiteFsRepository.h"

#include <gtest/gtest.h>

TEST(SQLiteFsRepository, tableExisting_false)
{
    SQLiteFsRepository repo("");
    
    EXPECT_FALSE(repo.doesHardLinksTableExisits());
    EXPECT_FALSE(repo.doesFoldersTableExists());
}

TEST(SQLiteFsRepository, createFoldersTable)
{
    SQLiteFsRepository repo("");
    
    repo.createFoldersTable();
    EXPECT_TRUE(repo.doesFoldersTableExists());
}

TEST(SQLiteFsRepository, createHardLinksTable)
{
    SQLiteFsRepository repo("");
    
    repo.createHardLinksTable();
    EXPECT_TRUE(repo.doesHardLinksTableExisits());
}

TEST(SQLiteFsRepository, getFolderCount)
{
    SQLiteFsRepository repo("");
    
    repo.createFoldersTable();
    EXPECT_EQ(0, repo.getFoldersCount());
}

TEST(SQLiteFsRepository, createFolder)
{
    SQLiteFsRepository repo("");

    repo.createFoldersTable();
    repo.addFolder();
    EXPECT_EQ(1, repo.getFoldersCount());
    
    repo.addFolder();
    EXPECT_EQ(2, repo.getFoldersCount());
}

TEST(SQLiteFsRepository, createHardLink)
{
    SQLiteFsRepository repo("");
    
    repo.createFoldersTable();
    repo.createHardLinksTable();
    
    std::vector<HardLink> hardLinks = repo.getHardLinks();
    EXPECT_EQ(0, hardLinks.size());
    
    repo.addFolderLink("test", 1, 0777, 0);
    hardLinks = repo.getHardLinks();
    
    ASSERT_EQ(1, hardLinks.size());
    
    EXPECT_EQ(HardLink::kFolder, hardLinks[0].nodeType);
    EXPECT_EQ("test",            hardLinks[0].name);
    EXPECT_EQ(1,                 hardLinks[0].nodeId);
    EXPECT_EQ(0,                 hardLinks[0].parentId);
    EXPECT_EQ(0777,              hardLinks[0].attributes);
    
    repo.addFolderLink("test1", 2, 0555, 3);
    hardLinks = repo.getHardLinks();

    ASSERT_EQ(2, hardLinks.size());
    
    EXPECT_EQ(HardLink::kFolder, hardLinks[1].nodeType);
    EXPECT_EQ("test1",           hardLinks[1].name);
    EXPECT_EQ(2,                 hardLinks[1].nodeId);
    EXPECT_EQ(3,                 hardLinks[1].parentId);
    EXPECT_EQ(0555,              hardLinks[1].attributes);
}

TEST(SQLiteFsRepository, getHardLinkInfo)
{
    SQLiteFsRepository repo("");
    
    repo.createHardLinksTable();
    
    int hardLinkId = repo.addFolderLink("test", 0, 0555, 0);
    HardLink hardLink = repo.getHardLinkInfo(hardLinkId);
    
    EXPECT_EQ(HardLink::kFolder, hardLink.nodeType);
    EXPECT_EQ("test",           hardLink.name);
    EXPECT_EQ(0,                 hardLink.nodeId);
    EXPECT_EQ(0,                 hardLink.parentId);
    EXPECT_EQ(0555,              hardLink.attributes);
}
